<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="快递名称" prop="expressId">
        <el-select v-model="form.expressId" filterable clearable>
          <el-option
            v-for="item in expressList"
            :key="item.id"
            :label="item.expressName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发货单号" prop="orderNo">
        <el-input v-model="form.orderNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="关联单号" prop="saleOrderNo">
        <el-input v-model="form.saleOrderNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="form.customerName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" clearable filterable>
          <el-option
            v-for="item in dict.type.ims_invoice_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发货日期" prop="time">
        <el-date-picker v-model="form.time" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" v-hasPermi="['ims:sendGood:okAll']" icon="el-icon-plus" plain
               @click="okAllSend">
      批量出单
    </el-button>
    <el-button type="success" v-hasPermi="['ims:sendGood:cancel']" icon="el-icon-printer" plain
               @click="okRepeat">
      复打
    </el-button>
    <el-button type="warning" v-hasPermi="['ims:sendGood:cancel']" icon="el-icon-remove-outline" plain
               @click="okCancel">
      撤单
    </el-button>
    <el-button type="primary" v-hasPermi="['ims:sendGood:express']" icon="el-icon-truck" plain
               @click="okExpress">
      快递修改
    </el-button>
    <el-button type="info" v-hasPermi="['ims:sendGood:export']" plain icon="el-icon-download"
               @click="handleExport"
    >导出
    </el-button>
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleChange"
      row-key="id"
    >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderNo" label="快递单号" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.orderNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="bagNum" label="包裹序号" align="center" width="80"/>
      <el-table-column prop="orderDate" label="发货日期" align="center" width="90"/>
      <el-table-column prop="expressName" label="快递名称" align="center" width="90"/>
      <el-table-column prop="saleOrderNo" label="关联单号" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="orderDetail(scope.row)">{{ scope.row.saleOrderNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="customerName" label="客户名称" align="center" width="100"/>
      <el-table-column prop="status" label="状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_invoice_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="发货明细" align="center" min-width="150"/>
      <el-table-column prop="realWeight" label="重量(kg)" align="center" width="100"/>
      <el-table-column prop="expressPrice" label="快递费用(元)" align="center" width="100"/>
      <el-table-column prop="recipient" label="收件人" align="center" width="90"/>
      <el-table-column prop="recipientTel" label="收件人手机号" align="center" width="100"/>
      <el-table-column prop="dstAddr" label="目的地" align="center"/>
      <el-table-column prop="remark" label="备注" align="center"/>
      <el-table-column label="操作" align="center" width="130">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <el-button
            type="text"
            v-if="scope.row.status==1"
            @click="updateRow(8,scope.row)"
            v-hasPermi="['ims:sendGood:split']"
          >拆单
          </el-button>
          <el-button
            type="text"
            v-if="scope.row.status==1"
            @click="updateRow(2,scope.row)"
            v-hasPermi="['ims:sendGood:ok']"
          >出单
          </el-button>
          <el-button
            type="text"
            v-if="scope.row.status==2||scope.row.status==10||scope.row.status==11"
            @click="updateRow(3,scope.row)"
            v-hasPermi="['ims:sendGood:send']"
          >发货
          </el-button>
          <el-button
            type="text"
            v-if="scope.row.status==3"
            @click="updateRow(4,scope.row)"
            v-hasPermi="['ims:sendGood:no']"
          >拦截
          </el-button>
          <el-button
            type="text"
            v-if="scope.row.status==7"
            @click="updateRow(5,scope.row)"
            v-hasPermi="['ims:sendGood:in']"
          >入库
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <sendDialog ref="sendDialog" @ok="getList"></sendDialog>
    <cancelDialog ref="cancelDialog" @ok="getList"></cancelDialog>
    <splitDialog ref="splitDialog" @ok="getList"></splitDialog>
    <detailDialog ref="detailDialog" @ok="getList"></detailDialog>
    <orderDetailDialog ref="orderDetailDialog"></orderDetailDialog>
    <outDialog ref="outDialog" @ok="getList"></outDialog>
    <expressDialog ref="expressDialog" @ok="getList"></expressDialog>
  </div>
</template>

<script>
import sendDialog from "./sendDialog.vue";
import outDialog from "./outDialog.vue";
import {
  listSendGood,
  noSendGood,
  inSendGood,
  okAllSendGood,
  listExpressShop,
  repeatSendGood
} from "../../../../api/expressMng";
import cancelDialog from "./cancelDialog.vue";
import detailDialog from "./detailDialog.vue";
import splitDialog from "./splitDialog.vue";
import orderDetailDialog from "/src/views/imsMng/orderMng/detailDialog.vue";
import {formatDay} from "../../../../utils";
import expressDialog from "../../saleMng/saleOrder/expressDialog.vue";


export default {
  components: {sendDialog, cancelDialog, detailDialog, splitDialog, orderDetailDialog, outDialog, expressDialog},
  dicts: ['ims_invoice_status'],
  data() {
    return {
      loading: false,
      list: [],
      form: {
        expressId: "",
        orderNo: "",
        saleOrderNo: "",
        customerName: "",
        status: "",
        time: [],
        pageNum: 1,
        pageSize: 10
      },
      selectList: [],
      selectIdList: [],
      expressList: [],
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      listExpressShop({pageSize: -1}).then(res => {
        this.expressList = res.rows;
      })
      this.handleQuery()
    },
    orderDetail(row) {
      let form = {}
      form.id = row.saleOrderId
      this.$refs.orderDetailDialog.show(form)
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    getList() {
      this.loading = true
      if (this.form.time && this.form.time.length) {
        this.form.startTime = this.form.time[0]
        this.form.endTime = this.form.time[1]
      } else {
        this.form.startTime = ''
        this.form.endTime = ''
      }
      listSendGood(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    handleChange(e) {
      this.selectList = e
      e.map(val => {
        this.selectIdList.push(val.id)
      })
    },
    okAllSend() {
      if (!this.selectList || !this.selectList.length) {
        this.$message.error('请先选择至少一条数据')
        return
      }
      this.$modal.confirm('是否确定批量出单').then(e => {
        okAllSendGood({ids: this.selectIdList}).then(res => {
          this.$message.success('批量出单成功')
          this.handleQuery()
        })
      })
    },
    okRepeat() {
      if (!this.selectList || this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      this.$modal.confirm('是否确定复打').then(e => {
        repeatSendGood({id: this.selectList[0].id}).then(res => {
          this.$message.success('复打成功')
          this.handleQuery()
        })
      })
    },
    okCancel() {
      if (!this.selectList || this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      let req = {
        id: this.selectList[0].id,
        saleOrderNo: this.selectList[0].saleOrderNo
      }
      this.$refs.cancelDialog.show(req)
    },
    okExpress() {
      if (!this.selectList || this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      let req = {
        id: this.selectList[0].saleOrderId,
        orderNo: this.selectList[0].saleOrderNo,
        expressId: this.selectList[0].expressId,
        recipientContent: this.selectList[0].recipientContent,
      }
      this.$refs.expressDialog.show(req)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/ims/invoice/export', {
        ...this.form
      }, `发货单_${formatDay(new Date())}.xlsx`)
    },
    updateRow(flag, row) {
      let form = JSON.parse(JSON.stringify(row))
      if (flag == 1) {
        this.$refs.detailDialog.show(form)
      }
      if (flag == 2) {
        this.$refs.outDialog.show(form)
      }
      if (flag == 3) {
        this.$refs.sendDialog.show(form)
      }
      if (flag == 4) {
        this.$modal.confirm('是否确定拦截').then(e => {
          noSendGood({id: row.id}).then(res => {
            this.$message.success('拦截成功')
            this.handleQuery()
          })
        })
      }
      if (flag == 5) {
        this.$modal.confirm('是否确定入库').then(e => {
          inSendGood({id: row.id}).then(res => {
            this.$message.success('入库成功')
            this.handleQuery()
          })
        })
      }
      if (flag == 8) {
        this.$refs.splitDialog.show(form)
      }
    }
  }
}
</script>
